package com.lvyq.javaprint.view.tray;

import com.lvyq.javaprint.constant.ImgConstant;
import com.lvyq.javaprint.constant.TrayConstant;
import com.lvyq.javaprint.util.RegistryUtil;
import com.lvyq.javaprint.view.style.STYLE;
import javafx.animation.TranslateTransition;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.StageStyle;
import javafx.util.Duration;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author lvyq 中间信息
 * @version 1.0
 * Create by 2024-04-29 14:15
 */
public class CenterView {

    public static Button versionBut;
    public static Button helpButton;

    public static ContextMenu contextMenu;
    /**
     * @description 版本按钮
     * @author lvyq
     * @throws
     * @return Button
     * @time 2024-04-29 14:27
     */
    public static Button createVersionBut() {
        versionBut = new Button("版本("+ TrayConstant.VERSION+")");
        versionBut.setAlignment(Pos.BASELINE_LEFT);

        versionBut.setPrefWidth(TrayConstant.TRAY_STAGE_WIDTH);
        versionBut.setGraphic(new ImageView(new Image(ImgConstant.TRAY_VERSION)));
        STYLE.BUTTON.toggle_style(versionBut);
        /*versionBut.getStyleClass().add("normal-button");

        versionBut.setOnMouseEntered(e ->{
            versionBut.getStyleClass().remove("normal-button");
            versionBut.getStyleClass().add("hover-button");
            contextMenu.hide();
        });
        versionBut.setOnMouseExited(e->{
            versionBut.getStyleClass().remove("hover-button");
            versionBut.getStyleClass().add("normal-button");
        });*/

        //打开网络地址
        versionBut.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton()==MouseButton.PRIMARY){
                    try {
                        Desktop.getDesktop().browse(new URI(TrayConstant.VERSINO_URL));
                    } catch (IOException |URISyntaxException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });
        return versionBut;
    }

    /**
     * @description 帮助按钮
     * @author lvyq
     * @throws
     * @return Button
     * @time 2024-04-29 14:29
     */
    public static Button createHelpBut() {

        // 创建菜单项
       /* MenuItem helpDoc = createHelpDocItem();
        MenuItem author = createAuthorItem();
        MenuItem qqGroup = createQQGroupItem();
        // 创建ContextMenu并添加菜单项
        contextMenu = new ContextMenu();
        //分割
        SeparatorMenuItem separator = new SeparatorMenuItem();
        contextMenu.getItems().addAll(helpDoc,separator,author);*/
        // 创建按钮
        helpButton = new Button("帮助");
        helpButton.setPrefWidth(TrayConstant.TRAY_STAGE_WIDTH);
        helpButton.setAlignment(Pos.BASELINE_LEFT);
        helpButton.setGraphic(new ImageView(new Image(ImgConstant.TRAY_HELP)));
        STYLE.BUTTON.toggle_style(helpButton);
        helpButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getButton()==MouseButton.PRIMARY){
                    try {
                        Desktop.getDesktop().browse(new URI(TrayConstant.PROJECT_URL));
                    } catch (IOException |URISyntaxException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        /*helpButton.getStyleClass().add("normal-button");
        helpButton.setOnMouseEntered(event -> {
            helpButton.getStyleClass().remove("normal-button");
            helpButton.getStyleClass().add("hover-button");
            contextMenu.show(helpButton, Side.RIGHT, 0, 0);
        });

        helpButton.setOnMouseExited(event -> {
            helpButton.getStyleClass().remove("hover-button");
            helpButton.getStyleClass().add("normal-button");
        });*/

        return helpButton;
    }



    /**
     * @description 创建开机 自启开关按钮
     * @author lvyq
     * @throws
     * @time 2024-05-11 11:52
     */
    public static HBox createSelfStartBut() throws IOException, InterruptedException{
        final boolean[] switchOn = {false};

        HBox hBox = new HBox(30);
        hBox.setStyle("-fx-padding: 5px;-fx-background-color: rgba(0.0,0.0,0.0,0.0)");
        Circle switchCircle = new Circle(8, javafx.scene.paint.Color.WHITE);
        switchCircle.setStroke(Color.WHITE);
        switchCircle.setStrokeWidth(2);

        Label label =  new Label("开机自启");
        StackPane switchPane = new StackPane(switchCircle);


        TranslateTransition transition = new TranslateTransition(Duration.seconds(0.2), switchCircle);
        String value = RegistryUtil.readRegistryValue(TrayConstant.RUN_KEY, TrayConstant.APP_NAME);
        if (value!=null){
            //设置位置
            switchCircle.setTranslateX(8);
            switchOn[0]=true;
        }else {
            //设置位置
            switchOn[0]=false;
            switchCircle.setTranslateX(-8);
        }
        if (switchOn[0]){
            switchPane.setStyle("-fx-cursor: hand;-fx-background-color:#13ce66;-fx-background-radius: 10");
        }else {
            switchPane.setStyle("-fx-cursor: hand;-fx-background-color: #CCCCCC;-fx-background-radius: 10");
        }
       // transition.setToX(0); // 设置目标位置在原始位置，即不移动

        switchPane.setPrefSize(36,20);
        switchPane.setMaxSize(36,20);
        switchPane.setOnMouseClicked(event -> {
            if (switchOn[0]) {
                transition.setToX(-8);
                transition.play();
                System.out.println("switchOn 关 = " + switchOn[0]);
                switchPane.setStyle("-fx-cursor: hand;-fx-background-color: #CCCCCC;-fx-background-radius: 10");
                try {
                    RegistryUtil.removeFromRegistry(TrayConstant.RUN_KEY,TrayConstant.APP_NAME);
                } catch (IOException |RuntimeException | InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    String APP_PATH =  System.getProperty("user.dir")+"/JPrint.exe";
                    APP_PATH =APP_PATH.replace("/","\\");
                    int addState = RegistryUtil.addToRegistry(TrayConstant.RUN_KEY, TrayConstant.APP_NAME, APP_PATH);
                    if (addState==1){
                        //失败
                        switchOn[0]=true;
                        createWaringAlter();
                    }else {
                        //成功
                        transition.setToX(8);
                        transition.play();
                        System.out.println("switchOn 开 = " + switchOn[0]);
                        switchPane.setStyle("-fx-cursor: hand;-fx-background-color: #13ce66;-fx-background-radius: 10");

                    }
                } catch (IOException |RuntimeException | InterruptedException e) {
                    e.printStackTrace();
                }

            }
            switchOn[0] = !switchOn[0];
        });

        hBox.getChildren().addAll(label,switchPane);
        return hBox;
    }



    /**
     * @description创建一个警告框
     * @author lvyq
     * @throw
     * @time 2024-05-14 14:14
     */
    private static void createWaringAlter() {
        // 创建一个警告框
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.initStyle(StageStyle.UTILITY);
        alert.setTitle("提示");
        alert.setHeaderText(null); // 不设置头部文本
        alert.setGraphic(new ImageView(ImgConstant.TRAY_VERSION));

        //创建一个水平布局，包含超链接和其他内容
        HBox content = new HBox();

        // 创建超链接
        Hyperlink hyperlink = new Hyperlink("手动设置");
        hyperlink.setOnAction(event -> {
            try {
                Desktop.getDesktop().browse(new URI(TrayConstant.PROJECT_URL));
            } catch (IOException |RuntimeException | URISyntaxException e) {
                    e.printStackTrace();
            }
        });
        hyperlink.setTranslateY(-3);

        Label label = new Label("设置失败,请关闭安全管家后再进行设置,或");
        content.getChildren().addAll(label,hyperlink);
        // 将垂直布局设置为警告框的内容
        alert.getDialogPane().setContent(content);
        // 显示警告框并等待用户响应
        alert.showAndWait()
                .filter(response -> response == ButtonType.OK) // 确保用户点击了确定按钮
                .ifPresent(response -> System.out.println("用户点击了确定按钮"));
    }
}
